const pool = require('../../mysql/Dao/sql')
const UserService = {
    login:async({username,password})=>{
   
        try{
            const params = [username,password]
            const sql = 'select * from user where username=? and password = ?'
            return await pool(sql,params)
        }
        catch(e){
          return { code:400,err:e}
        }
    },
    updata:async(data)=>{
        try{
            
            const params = [data.username,data.password,data.textarea,data.avator,data.id]
            const sql ='update user set username =?,password=?,textarea=?,avator=? where id =?'
            return await pool(sql,params)
        }
        catch(e){
            return {code:400,err:e}
        }
    },
    Userinformation:async(id)=>{
        try{        
                const params = [id]
       
                const sql = 'select * from user where id=?'
                return await pool(sql,params)
        }
        catch(e){
            return {code:400,err:e}
        }
    },
    Useradd:async(data)=>{
        try{
            const params=[data.username,data.password,data.type,data.textarea,data.avator]
            const sql = 'INSERT INTO user (username, password, type, textarea, avator)  values(?,?,?,?,?)'
            return await pool(sql,params)
        }
        catch(e){
            return {code:400,err:e}
        }
    },
    Getalluser:async(data)=>{
        try{
                const params=[]
                const sql='select * from user'
                return await pool(sql,params)
        }
        catch(e){
            return {code:400,err:e}
        }
    },
    Userdelete:async(id)=>{
        try{
            const params=[id]
            const sql='delete from user where id=?'
            return await pool(sql,params)
        }
        catch(err){
            return {
                code:400,
                err:err
            }
        }
    },
    Useredit:async(data)=>{
        try{
            const params = [data.username, data.password, data.type, data.id];
            const sql = 'UPDATE user SET username=?, password=?, type=? WHERE id=?';
            return await pool(sql, params);
        }
        catch(err){
            return{
                code:400,
                err:err
            }
        }
    }
}
module.exports =UserService